package com.tulei.qqread;


import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class BookVisual {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/admin";
    private static final String USER = "root";
    private static final String PASS = "123456";

    public static void main(String[] args) {
        List<Book> bookList = new ArrayList<>();

        // 查询数据库
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) {
            Statement stmt = conn.createStatement();
            String sql = "SELECT * FROM book";
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                String title = rs.getString("title");
                String author = rs.getString("author");
                String kind = rs.getString("kind");
                String intro = rs.getString("intro");
                String serial = rs.getString("serial");
                String count = rs.getString("count");
                Book book = new Book(title, intro, author,  kind, serial, count);
                bookList.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        // 统计每个类别的书籍数量和书籍名称并保存到excel
        QQReadUtil.saveExcel(bookList);



        Map<String, Integer> categoryCount = new HashMap<>();
        for (Book book : bookList) {
            String category = book.getKind();
            categoryCount.put(category, categoryCount.getOrDefault(category, 0) + 1);
        }

        // 创建数据集
        DefaultCategoryDataset barDataset = new DefaultCategoryDataset();
        for (Map.Entry<String, Integer> entry : categoryCount.entrySet()) {
            barDataset.addValue(entry.getValue(), "种类", entry.getKey());
        }

        Font font = Font.decode("SansSerif-PLAIN-12");

        // 创建一个标准的主题并设置字体
        StandardChartTheme theme = new StandardChartTheme("CN");
        theme.setExtraLargeFont(font);
        theme.setLargeFont(font);
        theme.setRegularFont(font);

        // 应用主题到JFreeChart
        ChartFactory.setChartTheme(theme);

        // 然后你就可以按照原来的代码创建和显示你的图表了
        JFreeChart barChart = ChartFactory.createBarChart(
                "书籍种类分布",
                "种类",
                "数量",
                barDataset,
                PlotOrientation.VERTICAL,
                true, true, false
        );

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("书籍种类分布图");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new ChartPanel(barChart));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });

    }
}
